package org.smart4j.framework.helper;

import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Service;
import org.smart4j.framework.util.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * 类操作助手类
 * @author luoyong
 * @date 2018-11-19下午 6:55
 */
public class ClassHelper {

    /**
     * 定义集合类(用于存放以加载的类)
     */
    private static final Set<Class<?>> CLASS_SET;

    static {
        String basePackage = ConfigHelper.getAppBasePackage();
        CLASS_SET = ClassUtil.getClassSet(basePackage);
    }

    /**
     *  获取应用包名下的所有类
     *
     */
    public static Set<Class<?>> getClassSet(){
        return CLASS_SET;
    }

    /**
     * 应用包名下的所有带有@Service注解的类
     *
     */
    public static Set<Class<?>> getServiceClassSet(){
        HashSet<Class<?>> classes = new HashSet<Class<?>>(16);
        for (Class cls : CLASS_SET){
            if (cls.isAnnotationPresent(Service.class)){
                classes.add(cls);
            }
        }
        return classes;
    }

    /**
     * 获取引用包名下的所有带@Controller的类
     *
     */
    public static Set<Class<?>> getControllerClassSet(){
        HashSet<Class<?>> classes = new HashSet<Class<?>>(16);
        for (Class cls : CLASS_SET){
            if (cls.isAnnotationPresent(Controller.class)){
                classes.add(cls);
            }

        }
        return classes;
    }

    /**
     *
     * 获取引用包名下的所有bean(包括service,controller)
     */
    public static Set<Class<?>> getBeanClassSet(){
        HashSet<Class<?>> beanClassSet = new HashSet<Class<?>>(16);
        beanClassSet.addAll(getControllerClassSet());
        beanClassSet.addAll(getServiceClassSet());
        return beanClassSet;
    }


}
